﻿using SqlSugar;

namespace Dev.DevNet.DbModel
{
  

    /// <summary>
    /// sqlsugar帮助类
    /// 生成实体帮助类
    /// </summary>
    public class SqlSugarDbFisterHelper
    {

       

        /// <summary>
        /// 获取config
        /// </summary>
        /// <returns></returns>
       
        public static ConnectionConfig DbConfig()
        {
            ConnectionConfig config = new ConnectionConfig()
            {
                ConnectionString = "server=localhost;port=3306;database=devdb;user id=root;password=Sasa123",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            };
            return config;
        }

        /// <summary>
        /// 数据库配置文件
        /// </summary>
        /// <param name="connstr">连接字符串</param>
        /// <param name="dbtype">数据库类型</param>
        /// <returns></returns>
        public static ConnectionConfig DbConfig(string connstr, DbType dbtype= DbType.MySql)
        {
            ConnectionConfig config = new ConnectionConfig()
            {
                ConnectionString = connstr,
                DbType = dbtype,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            };
            return config;
        }

        /// <summary>
        /// 根据数据库创建实体
        /// </summary>
        public static void DbFirstCreateModels()
        {
            Console.WriteLine("生成数据库实体...........开始");

           
            using (SqlSugarClient Client = new SqlSugarClient(DbConfig()))
            {
                foreach (var item in Client.DbMaintenance.GetTableInfoList())
                {
                    string entityName = item.Name.ToUpper();/*实体名大写*/
                    Client.MappingTables.Add(entityName, item.Name);
                    //foreach (var col in Client.DbMaintenance.GetColumnInfosByTableName(item.Name))
                    //{
                    //    Client.MappingColumns.Add(col.DbColumnName.ToUpper() /*类的属性大写*/, col.DbColumnName, entityName);
                    //}
                }


                //条件筛选--生成实体
                Client.DbFirst.SettingClassTemplate(o => { return StrHelper.SugarCustom.ClassTemplate; })
                    .SettingNamespaceTemplate(o => { return StrHelper.SugarCustom.NamespaceTemplate; })
                    .SettingClassDescriptionTemplate(o => { return StrHelper.SugarCustom.ClassDescriptionTemplate; })
                    .SettingConstructorTemplate(o => { return StrHelper.SugarCustom.ConstructorTemplate; })
                    .SettingPropertyDescriptionTemplate(o => { return StrHelper.SugarCustom.PropertyDescriptionTemplate; })
                    .SettingPropertyTemplate(o => { return StrHelper.SugarCustom.PropertyTemplate; })
                    .IsCreateAttribute(true).Where(c => c.StartsWith("dev")).CreateClassFile(@"E:\WooCodeVue\WooSolution\WooSolutionVue\Dev.DevNet.DbModel\Models");
                
            }

            Console.WriteLine("生成数据库实体...........结束");



        }
        /// <summary>
        /// 生成实体
        /// </summary>
        /// <param name="connstr">数据库连接字符串</param>
        /// <param name="EntityPath">生成实体路径</param>
        public static void  DbFirstCreateModel(string EntityPath,string connstr, DbType dbtype = DbType.MySql)
        {
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            Console.WriteLine("生成数据库实体...........开始");
            watch.Start();  //开始监视代码运行时间

            using (SqlSugarClient Client = new SqlSugarClient(DbConfig(connstr, dbtype)))
            {
                foreach (var item in Client.DbMaintenance.GetTableInfoList())
                {
                    string entityName = item.Name.ToUpper();/*实体名大写*/
                    Client.MappingTables.Add(entityName, item.Name);
                    //foreach (var col in Client.DbMaintenance.GetColumnInfosByTableName(item.Name))
                    //{
                    //    Client.MappingColumns.Add(col.DbColumnName.ToUpper() /*类的属性大写*/, col.DbColumnName, entityName);
                    //}
                }


                //条件筛选--生成实体
                Client.DbFirst.SettingClassTemplate(o => { return StrHelper.SugarCustom.ClassTemplate; })
                    .SettingNamespaceTemplate(o => { return StrHelper.SugarCustom.NamespaceTemplate; })
                    .SettingClassDescriptionTemplate(o => { return StrHelper.SugarCustom.ClassDescriptionTemplate; })
                    .SettingConstructorTemplate(o => { return StrHelper.SugarCustom.ConstructorTemplate; })
                    .SettingPropertyDescriptionTemplate(o => { return StrHelper.SugarCustom.PropertyDescriptionTemplate; })
                    .SettingPropertyTemplate(o => { return StrHelper.SugarCustom.PropertyTemplate; })
                    .IsCreateAttribute(true).Where(c => c.StartsWith("dev")).CreateClassFile(EntityPath);

            }
            watch.Stop();  //停止监视
            Console.WriteLine("生成数据库实体...........结束");
            TimeSpan timeSpan = watch.Elapsed;  //获取当前实例测量得出的总时间
            Console.WriteLine("生成实体用时：{0}(毫秒)", timeSpan.TotalMilliseconds);  //总毫秒数
            Console.ReadKey();
             //重新设置为零  
            watch.Reset();
            //重新设置并开始计时  
            watch.Restart();
            //结束计时  
            watch.Stop();
        }
      
}
}